编程之美 24点 LeetCode 679. 24 Game

265 篇文章 1 订阅
120 篇文章 1 订阅

Use sets to keep unique and Binary Form 1111 to the possible results.  The code is :

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
class Solution {
 public:
  set<double> status[16];
  typedef set<double>::iterator ITER;
  void f(int x, vector<int>& vec) {
    for (int i = 1; i <= x / 2; ++i) {
      if ((x & i) == i) {
        for (ITER j = status[i].begin(); j != status[i].end(); ++j)
          for (ITER k = status[x-i].begin(); k != status[x-i].end(); ++k) {
            status[x].insert((*j) * (*k));
            status[x].insert((*j) + (*k));
            status[x].insert((*j) - (*k));
            status[x].insert((*k) - (*j));
            if ((*k) != 0)
              status[x].insert((*j) / (*k));
            if ((*j) != 0)
              status[x].insert((*k) / (*j));
          }
      } 
    }
  }
  void init(const vector<int>& vec) {
    for (int i = 0; i < vec.size(); ++i)
      status[(1 << i)].insert((double)vec[i]);
  }
  bool getSum24(vector<int>& vec) {
    init(vec); 
    for (int i = 1; i <= 15; ++i)
      f(i, vec);
    return status[15].find(24) != status[15].end() ? true : false;
  }
};


int main() {
  int a[] = {1, 1, 1, 1};
  Solution s;
  vector<int> vec(a, a+4);
  bool res = s.getSum24(vec);

  return 0;
}

 

 

import math

class Test:
    def Sum24(self, nums):
        self.status = {i: set() for i in range(1, 16)}

        for i in range(4):
            self.status[1<<i].add(nums[i])
        for groupindex in range(1,16):
            for i in range(1, math.ceil((groupindex+1)/2)):
                if (i & groupindex == i):
                    subgroupindex = i
                    relativegroupindex = groupindex -subgroupindex
                    for k in self.status[subgroupindex]:
                        for l in self.status[relativegroupindex]:
                            self.status[groupindex].add(k + l)
                            self.status[groupindex].add(k - l)
                            self.status[groupindex].add(l - k)
                            self.status[groupindex].add(k * l)
                            if (k != 0):
                                self.status[groupindex].add(l / k)
                            if (l != 0):
                                self.status[groupindex].add(k / l)

        return self.status

test = Test()
res = test.Sum24([1,1,1,1])
print(24 in res[15])




You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.

Example 1:

Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24

 

Example 2:

Input: [1, 2, 1, 2]
Output: False

 

Note:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

--------------------------------------------------------------------

class Solution:
    def judgePoint24(self, nums) -> bool:
        l = len(nums)
        if (l != 4):
            return False
        set24 = [set() for i in range(16)]
        for i in range(4):
            idx = (1<<i)
            set24[idx].add(nums[i])

        for i in range(2,16):
            if not set24[i]:
                for j in range(1, (i>>1)+1):
                    if (j & i == j):
                        for a in set24[j]:
                            for b in set24[i-j]:
                                set24[i].add(a*b)
                                set24[i].add(a+b)
                                set24[i].add(a-b)
                                set24[i].add(b-a)
                                if (a != 0):
                                    set24[i].add(b/a)
                                if (b != 0):
                                    set24[i].add(a/b)
        for val in set24[15]:
            if abs(val-24) < 0.1:
                return True
        return False

s = Solution()
print(s.judgePoint24([1, 2, 1, 2]))

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值